<template>
  <el-form :model="form" label-width="180px" v-loading="isLoading">
    <el-form-item label="允许修改用户名">
      <el-switch v-model="form.isUserNameUpdateEnabled" />
    </el-form-item>
    <el-form-item label="允许修改邮箱">
      <el-switch v-model="form.isEmailUpdateEnabled" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit"> 保存 </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { onMounted, ref } from 'vue'

import { getUserRule, setUserRule } from '@/http/rule'

const form = ref({
  isUserNameUpdateEnabled: true,
  isEmailUpdateEnabled: true,
})
const isLoading = ref(false)

onMounted(() => {
  getData()
})

const getData = async () => {
  isLoading.value = true
  const res = await getUserRule().finally(() => {
    isLoading.value = false
  })
  form.value = res
}

const submit = async () => {
  isLoading.value = true
  await setUserRule(form.value).finally(() => {
    isLoading.value = false
  })
  ElMessage.success('保存成功')
}
</script>

<style scoped lang="scss"></style>
